
clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"
cd "${code}/tba_elig"

args index 

cap log close _all
log using "${log}/log-harp-`index'", name("log`index'") text replace

local MaxRunvar = 1
local MaxStage = 3
local MaxWindow = 3
local MaxNumPoly = 3
local MaxSample = 6

local RunvarType = ceil(`index'/ `MaxStage'/ `MaxWindow'/ `MaxNumPoly'/ `MaxSample')
if `RunvarType' == 1 {
	local Runvar "ltvprev_diff"
	local Cutoff "hi_ltvprev"
}

local index1 = `index' - (`RunvarType' - 1)* `MaxStage'* `MaxWindow'* `MaxNumPoly'* `MaxSample'
local Stage = ceil(`index1'/ `MaxWindow'/ `MaxNumPoly'/ `MaxSample')

local index2 = `index1' - (`Stage' - 1)* `MaxWindow'* `MaxNumPoly'* `MaxSample'
local WindowType = ceil(`index2'/ `MaxNumPoly'/ `MaxSample')
if `WindowType' == 1 local Window "inrange(`Runvar', -10, 10)"
if `WindowType' == 2 local Window "inrange(`Runvar', -7.5, 7.5)"
if `WindowType' == 3 local Window "inrange(`Runvar', -5, 5)"

local index3 = `index2' - (`WindowType' - 1)* `MaxNumPoly'* `MaxSample'
local NumPoly = ceil(`index3'/ `MaxSample')

local index4 = `index3' - (`NumPoly' - 1)* `MaxSample'
local SampleType = `index4'


di "Runvar=`Runvar'; Stage=`Stage'; Window=`WindowType'; NumPoly=`NumPoly'; Sample=`SampleType'"


*** simulated data
use "${temp}/gse_id-harp-cleaned.dta", clear

do _HARP-sampleselection // sample selection
do _HARP-spec

local SE "vce(robust)"

keep if `Window'
if `SampleType'==1 {
	keep if year(dofm(orig_ym)) >= 2012
}
if `SampleType'==2 {
	keep if year(dofm(orig_ym)) >= 2012
	su credit_scorecore_b, d
	keep if credit_scorecore_b <= `r(p50)'
	
}
if `SampleType'==3 {
	keep if year(dofm(orig_ym)) >= 2012
	su credit_scorecore_b, d
	keep if credit_scorecore_b <= `r(p50)'
	local SE "cluster(zip_3)"
	
}
if `SampleType'==4 {
	keep if year(dofm(orig_ym)) == 2012
}
if `SampleType'==5 {
	keep if year(dofm(orig_ym)) == 2012
	su credit_scorecore_b, d
	keep if credit_scorecore_b <= `r(p50)'
	
}
if `SampleType'==6 {
	keep if year(dofm(orig_ym)) >= 2012
	su credit_scorecore_b, d
	keep if credit_scorecore_b <= `r(p50)'
	local SE "cluster(orig_ym zip_3)"
	
}

// winsorize
qui su orig_rt, d
replace orig_rt = . if inrange(orig_rt, `r(p1)', `r(p99)')==0


gen Cutoff = `Cutoff'

gen slope = `Runvar' * Cutoff
global poly1 "`Runvar' c.`Runvar'#1.Cutoff"
global poly2 "$poly1 c.`Runvar'#c.`Runvar' c.`Runvar'#c.`Runvar'#1.Cutoff"
global poly3 "$poly2 c.`Runvar'#c.`Runvar'#c.`Runvar' c.`Runvar'#c.`Runvar'#c.`Runvar'#1.Cutoff"

*** regression
if `Stage' == 1 {
	reghdfe notba ${poly`NumPoly'} $controls, a(${FEs}) vce(robust)
}
if `Stage' == 2 {
	reghdfe orig_rt ${poly`NumPoly'} $controls, a(${FEs}) vce(robust)
}
if `Stage' == 3 {
	global poly1 ""
	global poly2 "c.`Runvar'#c.`Runvar' c.`Runvar'#c.`Runvar'#1.Cutoff"
	global poly3 "$poly2 c.`Runvar'#c.`Runvar'#c.`Runvar' c.`Runvar'#c.`Runvar'#c.`Runvar'#1.Cutoff"

	reghdfe orig_rt `Runvar' ${poly`NumPoly'} $controls (notba = slope), a(${FEs}) `SE'
}
estimate save "${Estimates}/harp-`Runvar'-Stage`Stage'-Window`WindowType'-Poly`NumPoly'-Sample`SampleType'", replace


*** Plot 
if inlist(`Stage', 1, 2) & `WindowType' == 1 & `NumPoly' == 3  {

preserve
	append using "${temp}/harp-simul-sample.dta" // this is for the simulation

	predict resid, r
	replace resid = 0 if simul == 1

	drop FE*
	local Poly ${poly`NumPoly'}
	foreach x in `Poly' {
		replace resid = resid + _b[`x']* (`x')
	}

	local DepVar = e(depvar)

	qui su `DepVar' if hi_ltvprev == 0 // make the average value below-cutoff the benchmark rate
	replace resid = resid + `r(mean)'
	
	replace ltvprev_diff = ceil(ltvprev_diff* 4)/4
	collapse resid, by(ltvprev_diff simul)
	
	// without predicted value
	if `NumPoly' == 3 {
	twoway scatter resid ltvprev_diff if simul~=1, xline(0) xtitle("MaxLTV-105") ytitle("") ylabel(, angle(0))
	graph export "${figure}/rdplot-harp-`DepVar'-Sample`SampleType'.pdf", replace
	}

restore
	
}

cap log close _all
